package com.microweb.toastmaster.webapp.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.beanutils.BeanUtils;

import com.microweb.toastmaster.Constants;
import com.microweb.toastmaster.model.TMRole;
import com.microweb.toastmaster.service.TMRoleManager;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class TMRoleController implements Controller {
    private final Log log = LogFactory.getLog(TMRoleController.class);
    private TMRoleManager tMRoleManager = null;

    public void setTMRoleManager(TMRoleManager tMRoleManager) {
        this.tMRoleManager = tMRoleManager;
    }

    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }

        TMRole tMRole = new TMRole();
        // populate object with request parameters
        BeanUtils.populate(tMRole, request.getParameterMap());

        List tMRoles = tMRoleManager.getTMRoles(tMRole);

        return new ModelAndView("tMRoleList", Constants.TMROLE_LIST, tMRoles);
    }
}
